package t_557

func reverseWords(s string) string {
	str := []byte(s)
	fast, slow := 0, 0

	for slow < len(str) {
		if string(str[slow]) == " " {
			slow++
			continue
		}

		// move fast to last pos fo the word
		for fast = slow; fast < len(str) && string(str[fast]) != " "; fast++ {
		}
		reverse(str, slow, fast-1)
		slow = fast
	}

	return string(str)
}

func reverse(str []byte, start int, end int) {
	for start < end {
		str[start], str[end] = str[end], str[start]
		start++
		end--
	}
}
